import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:go_router/go_router.dart';
import 'package:musician_flutter/main.dart';
import 'package:musician_flutter/ui/HomePage.dart';
import 'package:musician_flutter/ui/UserScreen.dart';
import 'package:musician_flutter/ui/artist_detail_page.dart';
import 'package:musician_flutter/ui/share_page.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../LayoutSwitcher.dart' show LayoutSwitcher;
import '../qr_widget.dart';

part 'route.g.dart';

const routerMain = '/';
const routerLayoutSwitcher = '/LayoutSwitcher';
const routerQRCodeWithText = '/QRCodeWithText';
const routerSharePage = '/SharePage';
const routerUserScreenPage = '/UserScreenPage';

final navigatorKey = GlobalKey<NavigatorState>();

@riverpod
GoRouter goRouter(Ref ref) {
  return GoRouter(
    initialLocation: routerMain,
    navigatorKey: navigatorKey,
    observers: [FlutterSmartDialog.observer],
    redirect: (BuildContext context, GoRouterState state) {
      return loginRedirect(ref, state);
    },
    routes: [
      GoRoute(
        path: routerMain,
        builder: (context, state) => const HomePage(),
      ),
      GoRoute(
        path: routerUserScreenPage,
        builder: (context, state) =>  UserScreen(),
      ),
      GoRoute(
        path: routerLayoutSwitcher,
        builder: (context, state) => const LayoutSwitcher(),
      ),
      GoRoute(
        path: routerQRCodeWithText,
        builder: (context, state) => const QRCodeWithText(
          url: 'ddd',
        ),
      ),
      GoRoute(
        path: routerSharePage,
        builder: (context, state) => const SharePage(),
      ),
    ],
    debugLogDiagnostics: true,
  );
}

String? loginRedirect(Ref ref, GoRouterState state) {
  final path = state.uri.path;
  // if (path == routerMain) {
  //   return routerMain;
  // }
  // if (path == routerLayoutSwitcher) {
  //   return routerLayoutSwitcher;
  // }
  // if (path == routerQRCodeWithText) {
  //   return routerQRCodeWithText;
  // }
  // if (path == routerSharePage) {
  //   return routerSharePage;
  // }
  return path;
}
//
// bool _needRedirectToLogin(String routePath) {
//   return routePath.startsWith(routerArtistDetail) || routePath.startsWith(routerMain);
// }
